<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use OT\DataDictionary;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController {

	//系统首页
    public function index(){

 
                 
        $this->display();
    }
    
    public function register(){
    	$this->display();
    }
		
    
    function login(){
    	$this->display();
    }
    
    public function partnerships(){
    	$this->display();
    }
    public function download(){
    	$this->display();
    }
    
    public function member(){
    	if(session('userInfo')){
    		$phone = session('userInfo')['phone'];
    		$userInfoMode = D('Userinfo');
    		$userinfo = $userInfoMode->where(['phone'=>$phone])->find();
    		$userinfo = array_merge( session('userInfo'),$userinfo);
    		$phone= $userinfo['phone'];
    		if($phone){
    			$model = D('AchievementInfo');
    			$basemodel =  D('AchievementBase');
    			$base_info =$basemodel->where(['phone'=>$phone])->find();
    			if($base_info){
    				$info = $model->where(['base_id'=>$base_info['id']])->select();
    				$this->fmtBaseInfo($base_info);
    				$this->fmtAchievementInfo($info);
    				$this->assign('base_info',$base_info);
    				$this->assign('cert_list',$base_info['cert_list']);
    				$this->assign('skill_list',$base_info['skill_list']);
    				$this->assign('info',$info);
    				$this->assign('userinfo',$userinfo);
    			}
    		}
    		
    		$this->display();
    	}else{
    		$this->display('login');
    	}
    }
    
    private function fmtBaseInfo(&$data){
    	$data['cert_list'] = explode(',', $data['cert_list']);
    	$data['skill_list'] = explode(',', $data['skill_list']);
    }
    
    private function fmtAchievementInfo(&$data){
    	foreach ($data as $key=>$value){
    		$data[$value['subject']]['stage_1'] = $data[$key]['stage_1'];
    		$data[$value['subject']]['stage_2'] = $data[$key]['stage_2'];
    		$data[$value['subject']]['stage_3'] = $data[$key]['stage_3'];
    		
    	}
    }
    
    public function doregister(){
    	$res = ['code'=>0,'msg'=>'Register Success'];
    	$f_name = I('f_name');
    	$l_name = I('l_name');
    	$pwd = I('pwd');
    	$email = I('email');
    	$phone = I('phone');
    	$gender = I('gender');
	    if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
    		$res['code']= -1;
    		$res['msg']= 'Mobile phone number error';
    		echo json_encode($res);
    		exit();
    	}
	    
    	if(!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)){
    		$res['code']= -1;
    		$res['msg']= 'E-mail address error';
    		echo json_encode($res);
    		exit();
    	}
    	
    	$user = D('user');
    	$data = [
			'f_name'=>$f_name,
			'l_name'=>$l_name,
			'mail'=>$email,
			'phone'=>$phone,
			'gender'=>$gender==1?'Male':'Female',
			'password'=>md5($pwd),
			'ctime'=>date('Y-m-d H:i:s')
    	];
    	if(!$user->add($data)){
    		$res['code']= -1;
    		$res['msg']= 'register error,Maybe the phone number has been registered';
    	}
    	echo json_encode($res);
    	exit();
    }
    
    function dologin(){
    	$res = ['code'=>0,'msg'=>'Login Success'];
    	$phone = I('phone');
    	$pwd = I('pwd');
    	if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
    		$res['code']= -1;
    		$res['msg']= 'Mobile phone number error';
    		echo json_encode($res);
    		exit();
    	}
    	
    	$user = D('user');
    	$userinfo = $user->where(['phone'=>$phone])->find();
    	if($userinfo){
    		if($userinfo['password']!=md5($pwd)){
    			$res['code']= -1;
    			$res['msg']= 'Password error';
    		}else{
    			session('userInfo',$userinfo);
    		}
    		echo json_encode($res);
    		exit();
    	}else{
    		$res['code']= -1;
    		$res['msg']= 'The phone number is not registered';
    		echo json_encode($res);
    		exit();
    	}
    	
    	 
    }
    
}